﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.JobCommand_工作中心;

namespace SmartProduction.Write.API.Application.CommandHandlers.JobCommandHandler_工作中心
{
    public class JobUpdateCommandHandler : IRequestHandler<JobUpdateCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Job> jobRepository;
        private readonly IMapper mapper;

        public JobUpdateCommandHandler(IBaseRepository<Job> jobRepository, IMapper mapper)
        {
            this.jobRepository = jobRepository;
            this.mapper = mapper;
        }
        public async Task<APIResult<object>> Handle(JobUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var entity=mapper.Map<Job>(request);
            var model=jobRepository.GetAll().Result.FirstOrDefault(x=>x.JId==request.JId);
            model.JId = request.JId;
            model.JobName = request.JobName;
            model.JobNode= request.JobNode; 
            model.JobType = request.JobType;
            model.Shift = request.Shift;
            model.ApprovalStatus = request.ApprovalStatus;
            model.UpdatedBy = request.UpdatedBy;
            model.UpdatedTime=DateTime.Now;
            var i=jobRepository.UpdateAsync(model).Result;
            if (i > 0)
            {
                result.Code = APIEnums.修改成功;
                result.Msg = APIEnums.修改成功.ToString();
                result.data = i;
            }
            else
            {
                result.Code = APIEnums.修改失败;
                result.Msg = APIEnums.修改失败.ToString();
                result.data = i;
            }
            return await Task.FromResult(result);
        }
    }
}
